{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'flow'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[3], line 17\u001b[0m\n\u001b[1;32m     13\u001b[0m \u001b[38;5;66;03m# addFlowToLink(G, 1, 2, 5)\u001b[39;00m\n\u001b[1;32m     14\u001b[0m \u001b[38;5;66;03m# addFlowToLink(G, 2, 3, 10)\u001b[39;00m\n\u001b[1;32m     16\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m u, v, data \u001b[38;5;129;01min\u001b[39;00m G\u001b[38;5;241m.\u001b[39medges(data\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m):\n\u001b[0;32m---> 17\u001b[0m     \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mEdge (\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mu\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m, \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mv\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m): Flow = \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mdata[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mflow\u001b[39m\u001b[38;5;124m'\u001b[39m]\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m     19\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m u, v, data \u001b[38;5;129;01min\u001b[39;00m G\u001b[38;5;241m.\u001b[39medges(data\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m):\n\u001b[1;32m     20\u001b[0m     data[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mflow\u001b[39m\u001b[38;5;124m'\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0\u001b[39m\n",
      "\u001b[0;31mKeyError\u001b[0m: 'flow'"
     ]
    }
   ],
   "source": [
    "import networkx as nx\n",
    "\n",
    "def addFlowToLink(G, u, v, flow):\n",
    "    if G.has_edge(u, v):\n",
    "        G[u][v]['flow'] += flow\n",
    "    else:\n",
    "        raise ValueError(f\"Edge ({u}, {v}) does not exist in the graph.\")\n",
    "\n",
    "G = nx.Graph()\n",
    "G.add_edge(1, 2, weight=10)\n",
    "G.add_edge(2, 3, weight=20)\n",
    "\n",
    "for u, v, data in G.edges(data=True):\n",
    "    data['flow'] = 0\n",
    "\n",
    "# addFlowToLink(G, 1, 2, 5)\n",
    "# addFlowToLink(G, 2, 3, 10)\n",
    "\n",
    "for u, v, data in G.edges(data=True):\n",
    "    print(f\"Edge ({u}, {v}): Flow = {data['flow']}\")\n",
    "\n",
    "for u, v, data in G.edges(data=True):\n",
    "    data['flow'] = 0\n",
    "\n",
    "for u, v, data in G.edges(data=True):\n",
    "    print(f\"Edge ({u}, {v}): Flow = {data['flow']}\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "xs",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
